//
//  CoreDataManager.m
//  DPGJ
//
//  Created by ZhuoLaiQiang@Gmail.com on 11-11-17.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "CoreDataManager.h"
#import "LFAccount.h"

@implementation CoreDataManager

@synthesize managedObjectContext     =       _managedObjectContext;

- (void)dealloc
{
    LF_RELEASE_SAFELY(_managedObjectContext);
    
    [super dealloc];
}

+ (CoreDataManager *)sharedCoreDataManager
{
    static CoreDataManager *shareManager = nil;
    
    if(!shareManager)
    {
        shareManager = [[CoreDataManager alloc] init];
    }
    
    return shareManager;
}

- (void)setManageObjectContext:(NSManagedObjectContext *)managedObject
{
    self.managedObjectContext = managedObject;
}

/* 保存coreData */
- (BOOL)saveChangesToCoreData
{
    if(!self.managedObjectContext){
        return NO;
    }
    
    NSError *error = nil;
    if([_managedObjectContext hasChanges]){
        
        if( ![_managedObjectContext save:&error]){
            
            NSLog(@"Failed to save to data store: %@", [error localizedDescription]);   
            NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];                
            if(detailedErrors != nil && [detailedErrors count]> 0) 
            {                       
                for(NSError* detailedError in detailedErrors) {
                    NSLog(@"  DetailedError: %@", [detailedError userInfo]);  
                }
            }else{
                NSLog(@"  %@", [error userInfo]);                
            }
            
            NSAssert(error == nil, @"Save Account to core data errror!");
            ITTDINFO(@"Save error!");
            
            return NO;
        }
    }
    
    return YES;
}

/* 获取用户资料 */
- (LFAccount *)getCurrentLoggedInAccount
{
    LFAccount* account = nil;
    
    if (self.managedObjectContext) 
    {
        NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"LFAccount" inManagedObjectContext:self.managedObjectContext];
        
        [request setEntity:entity];
        
        NSError *error = nil;
        NSArray *mutableFetchResults = [self.managedObjectContext executeFetchRequest:request error:&error];
        
        if (mutableFetchResults == nil || [mutableFetchResults count] == 0) 
        {
            ITTDINFO(@"No result, error: %@", [error description]);
        }else
        {
            account = [mutableFetchResults lastObject]; //为了后期加入多账户管理的功能，所以取最后一个
        }
    }
    
    return account;
}

- (void)clearAccountCoreData
{
    if (self.managedObjectContext) 
    {
        NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"LFAccount" 
                                                  inManagedObjectContext:self.managedObjectContext];
        
        [request setEntity:entity];
        
        NSError *error = nil;
        NSArray *mutableFetchResults = [self.managedObjectContext executeFetchRequest:request error:&error];
        
        for (int i = 0; i < [mutableFetchResults count]; i ++)
        {
            [self.managedObjectContext deleteObject: [mutableFetchResults objectAtIndex: i]];
        }
    }
    
    [self saveChangesToCoreData];
}

@end
